import { GET_USER_IDS } from '@/utils/const';
import userStore from '@/store/modules/user';

// 注入组件内数据
const componentData = ref({});
export const injectData = obj => {
  componentData.value = { ...componentData.value, ...obj };
};

// 模态框相关
export const showAdd = ref(false);
export const showEdit = ref(false);
export const activeRow = ref({});
export const handleAdd = () => {
  showAdd.value = true;
};

// 查询表单
export const searchParams = reactive({});

// 查询条件
export const formItems = reactive({
  name: {
    component: 'input',
    label: '姓名',
    placeholder: '请输入',
    maxlength: 100,
    input() {
      searchParams.user_ids = GET_USER_IDS(searchParams.name);
    }
  }
});

// 表格字段
export const columns = ref([
  { prop: 'id', label: '用户ID' },
  {
    prop: 'id',
    label: '姓名',
    format(data) {
      const userMap = userStore().userMap || {};
      return userMap[data]?.label;
    }
  },
  {
    prop: 'groups',
    label: '所属团队',
    format(data) {
      data = (data || []).map(item => item.name);
      return data.join('、');
    }
  },
  {
    prop: 'departments',
    label: '所属部门',
    format(data) {
      data = data || [];
      return data.join('、');
    }
  },
  {
    label: '操作',
    width: '100px',
    fixed: 'right',
    buttons: [
      {
        label: '编辑',
        click({ row }) {
          activeRow.value = row;
          showEdit.value = true;
        }
      }
    ]
  }
]);
